/**
 * Created by ray on 16/11/30.
 */
import Vue from 'vue'
// import Storage from '../storage/storage'
//import Aes from '../security/aes'

var urls = {}

urls.domain = 'http://127.0.0.1:9999'
//urls.domain = 'https://api.linkfun.win'

urls.getCat = urls.domain + '/cat/post/'


//打印输出
function myConsole(json) {
  console.log(json)
}

//字符串拼接成为url
function obj2uri(obj) {
  return Object.keys(obj).map(function (k) {
    return encodeURIComponent(k) + '=' + encodeURIComponent(obj[k]);
  }).join('&');
}

//Promise 工具
var Promise = function () {
  this.callbacks = [];
}

Promise.prototype = {
  construct: Promise,
  resolve: function (result) {
    this.complete("resolve", result);
  },
  reject: function (result) {
    
    this.complete("reject", result);
  },
  complete: function (type, result) {
    while (this.callbacks[0]) {
      var callBack = this.callbacks.shift()[type];
      if (typeof callBack == "function") {
        callBack(result)
      }
    }
  },
  then: function (successHandler, failedHandler) {
    this.callbacks.push({
      resolve: successHandler,
      reject: failedHandler
    });
    return this;
  }
}


var Api = {
    printTest: function () {
        console.log('akljshadkjashdkjas')
    },
    // 基础函数
    basePost: function (api_url, obj, succeedCallBack, failCallBack) {
        // 发送格式 body:{data:json}
        // 接收格式 body:{data:json}
        if (typeof obj != "object") {
            obj = {};
        }

        //请求数据
        // var userInfo = Storage.getUserInfo()
        // var commen = {};
        // commen.uId = String(userInfo.uId);
        // commen.token = String(userInfo.token);
        // commen.timeStamp = Date.parse(new Date());
        // commen.webVerson = '0';

        //真是数据
        var newObj = {};
        newObj.data = obj;
        //newObj.commen = commen;


        //发送数据
        var sendJson = JSON.stringify(newObj);

        myConsole('api_url: ' + api_url + ' sendJson: ' + sendJson);

        //Vue.http.post(api_url, Aes.aesEncrypt(sendJson), '')
        Vue.http.post(api_url, sendJson, '')
            .then(function (res) {

                if (typeof succeedCallBack == "function") {

                    //解码
                    //var json = Aes.aesDecrypt(res.body);
                    var json = res.body;

                    //json转object
                    var json_obj = JSON.parse(json);

                    myConsole(' api_url: ' + api_url + ' recJson: ' + JSON.stringify(json_obj));
                    succeedCallBack(json_obj);
                }

            }, function (res) {

                if (typeof failCallBack == "function") {
                    failCallBack();
                }

            })
    },
    // demo
    getCat: function (userName, password) {
        myConsole("login login login login login");
        var promise = new Promise()
        var obj = {}
        obj.userName = userName
        obj.passWord = password
        this.basePost(urls.getCat, obj, function (res) {
                promise.resolve(res)
            },
            function (res) {
                promise.reject(res)
            })
        return promise
    },
};

export default Api;

